<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru" xml:lang="ru"> 
    <head>
	<title>LocalCRM</title>
	<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
	<script src="js/jquery-ui-1.8.4.custom.min.js" type="text/javascript"></script>
	<script src="js/jquery.blockUI.js" type="text/javascript"></script>
	<script src="js/jquery.contextMenu.js" type="text/javascript"></script>

	<script src="js/i18n/grid.locale-ru.js" type="text/javascript"></script>
	<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>

	<script src="js/crm/common.js" type="text/javascript"></script>
	<link href="css/redmond/jquery-ui-1.8.4.custom.css" rel="stylesheet" type="text/css"/>
	<link href="css/jquery.contextMenu.css" rel="stylesheet" type="text/css"/>
	<link href="css/ui.jqgrid.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
	<script type="text/javascript">
	<!--{literal}
	$(document).ready
	(
	    function()
	    {
		var rate_id;

		$('table#rate-data-grid').jqGrid
		({
		    url:	'dict.php?name=phone_rate&data=yes',
		    datatype:	'json',
		    pager:	'rate-data-grid-pager',
		    colNames:	['ID','Название'],
		    height:	600,
		    colModel:
		    [
			{name:'id',width:30},
			{name:'rate_name',editable:true}
		    ],
		    editurl:	'dict.php?name=phone_rate&mode=edit',
		    onSelectRow:function(rateId,status)
				{
				    rate_id = rateId;

				    if ( status==true )
				    {
					$('table#zone-data-grid')
					    .jqGrid
					    (
						'setGridParam',
						{
						    url:	'dict.php?name=rate_zone&mode=list&rate_id='+rateId,
						    editurl:	'dict.php?name=rate_zone&mode=edit&phone_rate_id='+rateId,
						    cellurl:	'dict.php?name=rate_zone&mode=edit&phone_rate_id='+rateId,
						    page:	1
						}
					    )
					    .trigger('reloadGrid');

					$('table#phone-direction-grid')
					    .setGridParam
					    ({
						'url':	'dict.php?name=phone_direction&mode=list&phone_rate_id='+rate_id,
						'cellurl':	'dict.php?name=rate_zone&mode=edit&phone_rate_id='+rate_id
					    })
					    .trigger('reloadGrid');

					$('input#rate-edit')
					    .show()
					    .click
					    (
						function()
						{
						    $('table#rate-data-grid').jqGrid
						    (
							'editGridRow',
							rateId,
							{
							    reloadAfterSubmit:	true,
							    closeAfterAdd:	true,
							    addCaption:		'Изменить тариф'
							}
						    );
						}
					    );
				    }
				}
		});

		$('input#rate-new').click
		(
		    function()
		    {
			$('table#rate-data-grid').jqGrid
			(
			    'editGridRow',
			    'new',
			    {
				reloadAfterSubmit:	true,
				closeAfterAdd:		true,
				addCaption:		'Новый тариф'
			    }
			);
		    }
		);

		$('table#phone-direction-grid')
		    .jqGrid
		    ({
			datatype:	'json',
			pager:		'phone-direction-grid-pager',
			caption:	'Все направления в этом тарифе',
			cellEdit:	true,
//			cellurl:	'dict.php?name=rate_zone&mode=edit&phone_rate_id='+rate_id,
			beforeSaveCell:	function(id,name,value,iRow,iCol){if(!window.confirm('Сохранить новое значение для этого направления?')){$(this).jqGrid('restoreCell',{});}},
			afterSubmitCell:function(){$(this).trigger('reloadGrid');$('table#zone-data-grid').trigger('reloadGrid');},
			width:		800,
			height:		230,
			colNames:	['ID','Префикс','Направление','Тарифная зона'],
			colModel:
			[
			    {name:'id',hidden:true},
			    {name:'prefix',width:50},
			    {name:'title'},
			    {name:'zone_number',editable:true,width:30}
			]
		    })
		    .navGrid
		    (
			'#phone-direction-grid-pager',
			{
			    add:	false,
			    del:	false,
			    edit:	false,
			    refresh:	false
			},
			false,
			false,
			false,
			{
			    caption:	'Фильтр',
			    closeOnEscape:	true,
			    sopt:	['eq','bw','cn']
			}
		    );

		$('table#zone-data-grid')
		    .jqGrid
		    ({
			datatype:	'json',
			pager:		'zone-data-grid-pager',
			caption:	'Тарифные зоны',
			colNames:	['RateID','Зона','Стоимость'],
			width:		800,
			height:		280,
			cellEdit:	true,
			beforeSaveCell:	function(id,name,value,iRow,iCol){if(!window.confirm('Сохранить новое значение для этой зоны?')){$(this).jqGrid('restoreCell',{});}},
			afterSubmitCell:function(){$(this).trigger('reloadGrid');},
			colModel:
			[
			    {name:'phone_rate_id',hidden:true},
			    {name:'zone_number'},
			    {name:'cost',editable:true,editoptions:{defaultValue:'0.00'}}
			],
			subGrid:	true,
			subGridRowColapsed:	function(pID,rowID){$('#'+pID).empty();},
			subGridRowExpanded:	function(pID,rowID)
						{
						    var table = document.createElement('table');
						    var pager = document.createElement('div');

						    $(table).attr('id','t_'+pID);
						    $(pager).attr('id','p_'+pID);

						    $('#'+pID)
							.append(table)
							.append(pager);

						    $(table)
							.jqGrid
							({
							    url:	'dict.php?name=rate_zone&mode=list&rate_id='+rate_id+'&zone_number='+rowID,
							    datatype:	'json',
							    pager:	$(pager).attr('id'),
							    multiselect:true,
							    caption:	'Зона #'+rowID,
							    colNames:	['ID','Префикс','Направление'],
							    colModel:
							    [
								{name:'id',hidden:true,editable:true},
								{name:'prefix'},
								{name:'title'}
							    ],
							    sortname:	'title',
							    width:	640,
							})
							.navGrid
							(
							    '#p_'+pID,
							    {
								refresh:	false,
								edit:	false,
								add:	false,
								del:	false,
								search:	false
							    },
							    false,
							    false,
							    false,
							    {
								caption:	'Поиск направления',
								closeOnEscape:	true,
								sopt:		['eq','bw','cn']
							    }
							)
							.navButtonAdd
							(
							    '#p_'+pID,
							    {
								caption:	'Изменить зону',
								buttonicon:	'ui-icon-extlink',
								position:	'last',
								onClickButton:	function()
										{
										    var grid = $(this);
										    var selected = grid.getGridParam('selarrrow');

										    if ( selected.length>0 )
										    {
											$('div#rate-zone-change')
											    .dialog
											    (
												'option',
												{
												    title:	'Перенос направлений в другую зону',
												    resizable:	false,
												    buttons:
												    {
													'Изменить зону':	function()
																{
																    $.getJSON
																    (
																	'dict.php',
																	{
																	    name:	'rate_zone',
																	    mode:	'edit',
																	    item:	selected,
																	    new_zone:	$('input#zone-number-new').val()
																	},
																	function()
																	{
																	    $('div#rate-zone-change').dialog('close');
																	    grid.trigger('reloadGrid');
																	}
																    );
																}
												    }
												}
											    )
											    .dialog('open');
										    }
										}
							    }
							);
						}
		    })
		    .navGrid
		    (
			'#zone-data-grid-pager',
			{
			    add:	false,
			    edit:	false,
			    del:	false,
			    search:	false
			}
		    );
/*
		    .navButtonAdd
		    (
			'#zone-data-grid-pager',
			{
			    caption:	'Поиск направления',
			    position:	'last',
			    buttonicon:	'ui-icon-search',
			    onClickButton:	function()
						{
						    if ( rate_id>0 )
						    {
							$('table#phone-direction-grid')
							    .setGridParam({'url':'dict.php?name=phone_direction&mode=list&rate_id='+rate_id})
							    .trigger('reloadGrid');

							$('div#phone-direction')
							    .dialog
							    (
								'option',
								{
								    title:	'Поиск направления',
								    resizable:	false,
								    width:	$('table#phone-direction-grid').getGridParam('width')+20,
								}
							    )
							    .dialog('open');
						    }
						    else { alert('Вы не выбрали тариф!'); }
						}
			}
		    );
*/
	    }
	);
	{/literal}-->
	</script>

	{include file="admin/submodules/_menu.html"}

	<div id="rate-zone-change" class="dialog">
	    Номер зоны: <input type="text" id="zone-number-new"/>
	</div>

	<table>
	    <tr>
		<td>
		    <table id="rate-data-grid"></table>
		    <div id="rate-data-grid-pager"></div>
		</td>
		<td>
		    <table id="zone-data-grid"></table>
		    <div id="zone-data-grid-pager"></div>
		    <table id="phone-direction-grid"></table>
		    <div id="phone-direction-grid-pager"></div>
		</td>
	    </tr>
	</table>
	<br/>
	<input type="button" class="button" value="Создать тариф" id="rate-new"/>
	<input type="button" class="button" value="Редактировать" id="rate-edit" style="display:none;"/>

    </body>
</html>
